<template>
  <van-datetime-picker
    v-model="currentDate"
    type="date"
    title="选择年月日"
    :min-date="minDate"
    :max-date="maxDate"
    @confirm="onConfirm"
    @cancel="onCancel"
  />
</template>

<script>
import { editUserProfileAPI } from '@/api'
import dayjs from '@/utils/day.js'
//1.回显数据  2.取消重置  3.确认修改
export default {
  props: {
    birthday: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      minDate: new Date(1900, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.birthday)
    }
  },
  methods: {
    async onConfirm() {
      const birthday = dayjs(this.currentDate).format('YYYY-MM-DD')
      //1.发送请求
      await editUserProfileAPI({
        birthday
      })
      //2.修改视图
      this.$emit('update:birthday', birthday)
      //3.关闭弹层
      this.$emit('close')
    },
    onCancel() {
      //1.关弹层
      this.$emit('close')
      //2.通过v-if来控制子组件的销毁
    }
  }
}
</script>

<style></style>
